﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Prod;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainProductPlanOrderSplit : BaseForm.frmBaseDialogyMaintainForm
    {
        public frmMaintainProductPlanOrderSplit()
        {
            InitializeComponent();
        }
        #region vars
        private ProdPlanOrderEntity mSelectedPlanOrder;
        #endregion

        #region ctors
        public frmMaintainProductPlanOrderSplit(ProdPlanOrderEntity m)
        {
            InitializeComponent();
            mSelectedPlanOrder = m;
        }

        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PSTATUS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbSplitStatus, "PSTATUS", false);

            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value", typeof(int));
            dt.Rows.Add("否", 0);
            dt.Rows.Add("是", 0);
            cmbIsMark.DataSource = dt;
            cmbIsMark.DisplayMember = "name";
            cmbIsMark.ValueMember = "value";
            cmbSplitIsMark.DataSource = dt;
            cmbSplitIsMark.DisplayMember = "name";
            cmbSplitIsMark.ValueMember = "value";
        }

        private void SetField()
        {
            if (mSelectedPlanOrder == null)
                return;
            SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mSelectedPlanOrder.ApplicationItemId);
            if (mApplicationItem == null)
            {
                MessageBox.Show("生产计划申请单为空，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SalesProdPlanApplicationEntity mApplication = SalesProdPlanApplicationBs.GetSalesProdPlanApplicationByID(mApplicationItem.ApplicationId);
            if (mApplication == null)
            {
                MessageBox.Show("生产计划申请单为空，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mApplicationItem.ProductId);
            if (mProduct == null)
            {
                MessageBox.Show("待生产的成品编码不存在，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            txtApplicationOrder.Text = mApplication.ApplicationCode;
            txtProductPlanOrder.Text = mSelectedPlanOrder.PlanOrderCode;
            txtPlanQty.Text = mSelectedPlanOrder.Qty.ToString();
            txtProductName.Text = mProduct.ProductName;
            txtProductCode.Text = mProduct.ProductCode;
            txtDescription.Text = mProduct.Description;
            txtContractModel.Text = mApplicationItem.ProductSpecOut;
            dtpPlanDeliveryDate.EditValue = mSelectedPlanOrder.DeliveryDate;
            cmbStatus.SetSelectedValue(mSelectedPlanOrder.Status);
            cmbIsMark.SetSelectedValue(mSelectedPlanOrder.IsMark);
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtSplitQty.Text) == true)
            {
                MessageBox.Show("拆分数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSplitQty.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(dtpSplitPlanDeliveryDate.EditValue.ToString()) == true)
            {
                MessageBox.Show("计划交期不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dtpSplitPlanDeliveryDate.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(cmbSplitStatus.SelectedValue.ToString()) == true)
            {
                MessageBox.Show("状态不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbSplitStatus.Focus();
                return false;
            }
            if (Convert.ToDecimal(txtSplitQty.Text) > mSelectedPlanOrder.Qty)
            {
                MessageBox.Show("拆分数量不可大于原计划单数量！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSplitQty.Focus();
                return false;
            }
            return true;
        }

        private bool SplitOrder()
        {
            if (mSelectedPlanOrder == null)
            {
                MessageBox.Show("生产计划单不存在，无法拆单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (CheckData() == false)
            {
                return false;
            }
            //拆分原先的生产计划单
            ProdPlanOrderEntity mSplitOrder = new ProdPlanOrderEntity();
            mSplitOrder.PlanOrderId = Guid.NewGuid().ToString();
            mSplitOrder.ApplicationItemId = mSelectedPlanOrder.ApplicationItemId;
            mSplitOrder.BlueprintId = mSelectedPlanOrder.BlueprintId;
            mSplitOrder.Qty = Convert.ToDecimal(txtSplitQty.Text);
            mSplitOrder.DeliveryDate = (DateTime)dtpSplitPlanDeliveryDate.EditValue;
            mSplitOrder.PlanOrderPersn = Common.CommonParameter.CurrentUser.PersonId;
            mSplitOrder.PlanOrderDate = DateTime.Now;
            mSplitOrder.CreatedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSplitOrder.CreatedDate = DateTime.Now;
            mSplitOrder.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSplitOrder.LastModifiedDate = DateTime.Now;
            mSplitOrder.Status = cmbSplitStatus.SelectedValue.ToString();
            mSplitOrder.IsMark = Convert.ToInt32(cmbSplitIsMark.SelectedValue.ToString());

            mSelectedPlanOrder.Qty = mSelectedPlanOrder.Qty - Convert.ToDecimal(txtSplitQty.Text);
            if (ProdPlanOrderBs.SplitProductPlanOrder(mSelectedPlanOrder, mSplitOrder))
            {
                return true;
            }
            else
            {
                MessageBox.Show("拆单失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            SetField();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SplitOrder())
            {
                MessageBox.Show("拆单成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion
    }
}
